package com.group15.phms;

import java.util.ArrayList;
import java.util.List;


import com.group15.phms.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        GridView gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new MyAdapter(this));
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_screen, menu);
        return true;
    }
    Intent intent;
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_logout:
			intent = new Intent(this, Login.class);
	    	startActivity(intent);
	    	finish();
			return true;
		case R.id.action_search:
			intent = new Intent(this, Search.class);
	    	startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
    
    
    private class MyAdapter extends BaseAdapter
    {
        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;

        public MyAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);
            
            items.add(new Item("Vital Signs", R.drawable.heart));
            items.add(new Item("Medication", R.drawable.medicine));
            items.add(new Item("Notes", R.drawable.file));
            items.add(new Item("Diet Plan", R.drawable.clipboard));
            items.add(new Item("My Info", R.drawable.settings));
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).drawableId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
               v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
               v.setTag(R.id.picture, v.findViewById(R.id.picture));
               v.setTag(R.id.text, v.findViewById(R.id.text));
               v.setId(i);
               v.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						handleClicks(view);
					}
				});
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setImageResource(item.drawableId);
            name.setText(item.name);

            return v;
        }
        

        private class Item
        {
            final String name;
            final int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
    }
	public void handleClicks(View view)
    {
    	if (view.getId()==0)
    	{
    		Intent intent = new Intent(this, VitalSignsActivity.class);
        	startActivity(intent);
    	}
    	if (view.getId()==1)
    	{
    		Intent intent = new Intent(this, Medications.class);
        	startActivity(intent);
    	}
    	if (view.getId()==2)
    	{
    		Intent intent = new Intent(this, Notes.class);
        	startActivity(intent);
    	}
    	if (view.getId()==3)
    	{
    		
        	Intent intent = new Intent(this, Diet.class);
        	startActivity(intent);
    	}
    	if (view.getId()==4)
    	{
    		
        	Intent intent = new Intent(this, UserInfo.class);
        	startActivity(intent);
    	}
    		
    }
    
    
    
    /** Called when the user clicks the Vital Signs icon */
    public void vitalSigns(View view) {
    	
    	Intent intent = new Intent(this, VitalSignsActivity.class);
    	startActivity(intent);
    }
    /** Called when the user clicks the Medication icon */
    public void medication(View view) {
		Intent intent = new Intent(this, Medications.class);
    	startActivity(intent);
        
    }
    
    /** Called when the user clicks the Diet Plan icon */
    public void diet(View view) {
		Intent intent = new Intent(this, Diet.class);
    	startActivity(intent);
        
    }
    
    /** Called when the user clicks the Notes icon */
    public void notes(View view) {
		Intent intent = new Intent(this, Notes.class);
    	startActivity(intent);
        
    }
  
}
